package com.caterina.components;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JTextField;

import com.caterina.resources.CBorders;
import com.caterina.resources.CColors;
import com.caterina.resources.CFont;

public class CTextField extends JTextField{
	public CTextField(String text,int size){
		this(text,size,"");	
	}
	public CTextField(String text,int size,String emptyText){
		super(text,size);
		this.setFont(CFont.CTEXT_STANDARD);
		this.setSelectedTextColor(CColors.LIST_ENTRY_HIGHLIGHT_TITLE_FOREGROUND);
		this.setSelectionColor(CColors.LIST_ENTRY_HIGHLIGHT_BACKGROUND);
		this.setBorder(CBorders.FIELDS_BORDER);
		this.setToolTipText(emptyText);
		this.emptyText=emptyText;
		if (text.equals("")){
			this.setText(emptyText);
			CTextField.this.setFont(new Font("Tahoma",Font.ITALIC,15));
			this.setForeground(Color.LIGHT_GRAY);
		}else this.setText(text);
		cleateListener();
	}
	public CTextField(String text,String tag,int size,String emptyText){
		this(text,size,emptyText);
	}
	public String getText(){
		if (this.getForeground().equals(Color.LIGHT_GRAY)) return "";
		else return super.getText();
	}
	private void cleateListener(){
		CTextField.this.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent arg0) {
				if (CTextField.this.getForeground().equals(Color.LIGHT_GRAY)){
					CTextField.this.setFont(new Font("Tahoma",Font.PLAIN,15));
					CTextField.this.setForeground(Color.black);
					CTextField.this.setText("");
				}
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				if (CTextField.this.getForeground().equals(Color.black)&&CTextField.super.getText().equals("")){
					CTextField.this.setFont(new Font("Tahoma",Font.ITALIC,15));
					CTextField.this.setForeground(Color.LIGHT_GRAY);
					CTextField.this.setText(CTextField.this.emptyText);
				}
			}
			});
	}
	private String emptyText;
	private static final long serialVersionUID = 1L;
}
